﻿using System.Drawing.Printing;

using pt=System.UInt32;

namespace RDLC
{  
   public class PageSetup
   {
      public enum PageOrientation { Portrait, Landscape };

      public const pt LetterPageWidth = 612; // 8.5 in
      public const pt LetterPageHeight = 792; // 11 in

      public pt PageWidth = LetterPageWidth; 
      public pt PageHeight = LetterPageHeight;

      public const pt HALF_INCH = 36;
      public const pt ONE_INCH = 72;

      public pt LeftMargin
      {
         get { return _LeftMargin; }
         set
         {
            _LeftMargin = value;
            _InnerPageWidth = PageWidth - _LeftMargin - _RightMargin;
            _InnerPageCenter = _InnerPageWidth/2;
         }
      }

      private pt _LeftMargin = HALF_INCH;

      public pt RightMargin
      {
         get { return _RightMargin; }
         set
         {
            _RightMargin = value;
            _InnerPageWidth = PageWidth - _LeftMargin - _RightMargin;
            _InnerPageCenter = _InnerPageWidth/2;
         }
      }

      private pt _RightMargin = HALF_INCH;

      public pt TopMargin
      {
         get { return _TopMargin; }
         set
         {
            _TopMargin = value;
            _InnerPageHeight = PageHeight - _BottomMargin - _TopMargin;
         }
      }

      private pt _TopMargin = HALF_INCH;

      public pt BottomMargin
      {
         get { return _BottomMargin; }
         set
         {
            _BottomMargin = value;
            _InnerPageHeight = PageHeight - _BottomMargin - _TopMargin;
         }
      }

      private pt _BottomMargin = HALF_INCH;

      public pt InnerPageWidth
      {
         get { return _InnerPageWidth; }
      }
      private pt _InnerPageWidth = LetterPageWidth - ONE_INCH;

      public pt InnerPageHeight
      {
         get { return _InnerPageHeight; }
      }
      private pt _InnerPageHeight = LetterPageHeight - ONE_INCH;

      public pt InnerPageCenter
      {
         get { return _InnerPageCenter; }
      }
      private pt _InnerPageCenter = (LetterPageWidth - ONE_INCH) / 2;

      public PageOrientation Orientation
      {
         get { return _Orientation; }
         set
         {
            if((_Orientation == value))
               return;

            _Orientation = value;
            if(_Orientation == PageOrientation.Landscape)
            {
               PageWidth = LetterPageHeight;
               PageHeight = LetterPageWidth;
            }
            else
            {
               PageWidth = LetterPageWidth;
               PageHeight = LetterPageHeight;
            }

            _InnerPageWidth = PageWidth - _LeftMargin - _RightMargin;
            _InnerPageHeight = PageHeight - _TopMargin - _BottomMargin;

            _InnerPageCenter = _InnerPageWidth/2;
         }
      }

      private PageOrientation _Orientation = PageOrientation.Portrait;

      public static implicit operator PageSettings(PageSetup pageSetup)
      {
         // PageSetup units are in pts. Divide by .72 to convert to 100ths of an inch.

         var pageSettings = new PageSettings
                               {
                                  Landscape = (pageSetup.Orientation == PageOrientation.Landscape),
                                  Margins =
                                     new Margins((int)(pageSetup.LeftMargin/0.72), (int)(pageSetup.RightMargin/0.72),
                                                 (int)(pageSetup.TopMargin/0.72), (int)(pageSetup.BottomMargin/0.72)),
                               };

         return pageSettings;
      }

      public static explicit operator PageSetup(PageSettings pageSettings)
      {
         // PageSettings units are in 100ths of an inch. Multiply by .72 to convert to pts

         var pageSetup = new PageSetup
                            {
                               BottomMargin = (pt)(pageSettings.Margins.Bottom * 0.72),
                               LeftMargin = (pt)(pageSettings.Margins.Left * 0.72),
                               PageHeight = (pt)(pageSettings.PaperSize.Height * 0.72),
                               PageWidth = (pt)(pageSettings.PaperSize.Width * 0.72),
                               TopMargin = (pt)(pageSettings.Margins.Top * 0.72),
                               RightMargin = (pt)(pageSettings.Margins.Right * 0.72),
                               Orientation = pageSettings.Landscape
                                                ? PageOrientation.Landscape
                                                : PageOrientation.Portrait
                            };

         return pageSetup;
      }
   }
}
